// pages/user/index.js
import { request } from "../../request/index.js";
import regeneratorRuntime from "../../lib/runtime/runtime";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[
      {id:0,icon:"iconfont icon-shou_cang",title:"我的收藏",isActive:true},
      {id:1,icon:"iconfont icon-bag",title:"我的失物招领",isActive:false},
      {id:2,icon:"iconfont icon-Notes",title:"我的新鲜事",isActive:false}
    ],
    //我的收藏数组
    myColList:[],
    //我的失物招领数组
    myLostFound:[],
    //我的新鲜事数组
    myNewThing:[],
    //用户ID
    wx_user_id:'',
    //被收藏的状态
    isCollect: true,
    //---测试---
    userInfo:{},
  },
  handleTabsItemChange(e) {
    // console.log(e.detail);
    //获取传递过来的数据
    const { index } = e.detail;
    //获取原数组，修改源数组

    let { list } = this.data;
    //遍历list,如果index等于数组中的index,则修改数组中的isActive
    list.forEach((v, i) =>
      i === index ? (v.isActive = true) : (v.isActive = false)
    );
    this.setData({
      list,
    });
  },

  

  // 登录按钮点击登录
  // denglu(){
  //   console.log('点击了登录按钮')
  //   wx.login({
  //     success: res => {
  //       // 发送 res.code 到后台换取 openId, sessionKey, unionId
  //       console.log('获取res',res)
  //       if (res.code) {
  //         // wx.request({
  //         //   url: 'https://api.weixin.qq.com/sns/jscode2session',
  //         //   data:{
  //         //     appid:'wx382a2e5d86eb7006',
  //         //     secret:'32788e23bc333c96fcd83bcf578f76e4',
  //         //     js_code:res.code,
  //         //     grant_type:'authorization_code'
  //         //   },
  //         //   success(res){
  //         //     console.log(res);
  //         //   }
  //         // })
  //         //发起网络请求
  //         wx.request({
  //           url: 'http://172.17.26.211:3337/user/onlogin',
  //           data: {
  //             code:res.code,
  //             phone:'18612345678',
  //             password:'123456'
  //            },
  //           success:(res)=>{
  //               console.log(res)
  //           }
  //         })
  //       } else {
  //         console.log('登录失败！' + res.errMsg)
  //       }
  //     }
  //   })
  // },


  async handleCollect(e) {
    //获取用户收藏数据
    // const res = await request({ url: "/user/myFavorites?wx_user_id=1" });
    // let collectArr = res.data.data;

    let id = e.currentTarget.dataset.id;
    let collectArr = this.data.myColList;

    let isCollect = collectArr.findIndex((v) => v.active_id === id);

    if (isCollect == -1) {
      // 判断产品是否存在收藏列表中，如果不存在则加入收藏
      this.Collect(id);
      this.myColList();
    } else {
      // 判断产品是否存在收藏列表中，如果存在则取消收藏
      this.DelCollect(id);
      this.myColList();

    }
  },

  //加入收藏
  async Collect(params) {
    //campus_activites_id
    let id = params;
    let userId = 1;
    const res = await request({
      url:
        "/activites/addFavorites?campus_activites_id=" +
        id +
        "&wx_user_id=" +
        userId +
        "",
    });
  },

  //取消收藏
  async DelCollect(params) {
    let id = params;
    let userId = 1;
    const res = await request({
      url:
        "/activites/unFavorite?campus_activites_id=" +
        id +
        "&wx_user_id=" +
        userId +
        "",
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
      this.myColList();
      this.myLostFound();
      this.myNewThing();
      this.setData({
        userInfo:wx.getStorageSync("userInfo")
      })
  
  },
  //获取我的收藏列表
  async myColList(){
    const res = await request({url:"/user/myFavorites?wx_user_id=1" })
    console.log('我的收藏列表',res.data.data)
    this.setData({
        myColList:res.data.data
    })
  },
  //获取我的失物招领列表
  async myLostFound(){
    const res = await request({url:"/user/myLastFound?wx_user_id=1" })
    console.log('我的失物招领列表',res.data.data)
    let changeTimeList=res.data.data
    changeTimeList.forEach((v)=>{
        // console.log(v)
        v.create_time=this.timer(v.create_time)
        // console.log(v.create_time)
    })
    this.setData({
      myLostFound:changeTimeList
    })
  },
  //获取我的新鲜事列表
  async myNewThing(){
    const res = await request({url:"/user/myNewThing?wx_user_id=1" })
    console.log('我的新鲜事列表',res.data.data)
    let changeTimeList=res.data.data
    changeTimeList.forEach((v)=>{
        // console.log(v)
        v.create_time=this.timer(v.create_time)
        // console.log(v.create_time)
    })
    this.setData({
      myNewThing:changeTimeList
    })
  },

  //将时间戳转换为标准时间
  timer(t){
    //年-月-日-时-分-秒
    let now = new Date(t)
    let year = now.getFullYear();
    let month = now.getMonth() + 1;
    let date = now.getDate();
    let hour = now.getHours();
    let minute = now.getMinutes();
    let second = now.getSeconds();
    // 补零
    if (month < 10) {
      month = "0" + month;
    }
    if (date < 10) {
      date = "0" + date;
    }
    if (hour < 10) {
      hour = "0" + hour;
    }
    if (minute < 10) {
      minute = "0" + minute;
    }
    if (second < 10) {
      second = "0" + second;
    }
    return (
      //按需求拼接   
      year + "-" + month + "-" + date
      // year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second
    );
   },
})